package org.dizem.freepl0.gui.panel;

import org.dizem.freepl0.gui.controller.FPController;

import javax.swing.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: DIZEM
 * Date: 11-2-2
 * Time: 下午3:59
 */
public class TabPanel extends ClosableTabPanel {

	private FPController controller;

	public TabPanel(FPController controller) {
		this.controller = controller;
	}

	public void addNewTab(File file) {
		try {
			FileReader reader = new FileReader(file);
			char[] buf = new char[1024];
			int len = reader.read(buf);
			if (len != -1)
				addNewTab(new String(buf, 0, len), file.getName());
			else
				addNewTab("", file.getName());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void addNewTab(String code, String filename) {
		add(new JScrollPane(new EditPanel(code, filename)), filename + "  ");
	}

	public EditPanel getCurrentTab() {
		return (EditPanel) ((JScrollPane) getSelectedComponent()).getViewport().getView();
	}
}
